<template>
  <tl-sub-page :render="render" :visible.sync="visible" v-dialogDrag :title="title" :close-on-click-modal="false" class="tl-dialog" width="1200px" @open="open" @close="close">
    <div class="mpm-project-container">
      <div class="mpm-project-header">
        <div class="info">
          <img src="/static/images/paojectManager/mpmscore.png" class="logo" alt="项目风险" style="width:55px;height:55px" />
          <div class="content" :title="model.projectname">
            <p>
              <b>评标专家拜访反馈</b>
            </p>
            <p class="light" v-show="model.id">
              <span>申请人：{{ model.username + '【' + model.usercode + '】' }}</span>
              <span>创建时间：{{ formatDate(model.createon) }}</span>
            </p>
          </div>
        </div>
        <div class="toolbar">
          <el-button type="danger" icon="el-icon-close" circle @click="doCancelEdit"></el-button>
        </div>
      </div>
      <div class="mpm-project-nav" :style="{ height: mainHeight + 'px' }">
        <ul class="nav-list">
          <template v-for="item in navList">
            <li :key="item.code" :class="item.on ? 'on' : ''" @click="clickNav(item.code)" style="cursor: pointer;clear: both">
              <span style="float: left;width: 150px">{{ item.name }}</span>
              <span style="float: left;">
                <i v-if="item.count > 0">{{ item.count }}</i>
                <label v-if="item.flag">{{ item.flag }}</label>
              </span>
            </li>
          </template>
        </ul>
      </div>
      <div class="mpm-project-main" :style="{ height: mainHeight + 'px' }">
        <el-scrollbar class="mpm-project-main-container" id="mpmMainContainer">
          <div style="padding: 0 17px">
            <el-form class="tl-form" ref="formable" :model="model" :rules="rules" label-width="150px">
              <div class="tl-form-line-title">
                填写内容
              </div>
              <a id="basic" class="step-position">&nbsp;</a>
              <el-row>
                <el-col :span="12">
                  <el-form-item label="是否有效" prop="canreal" :rules="rules.validateRequire">
                    <el-select v-model="model.canreal" filterable clearable>
                      <el-option v-for="item in codeModel.enabledList" :key="item.code" :label="item.name" :value="item.code" />
                    </el-select>
                  </el-form-item>
                </el-col>
                <el-col :span="24">
                  <el-form-item label="建议内容" prop="advisecomment" :rules="model.canreal === '0' ? rules.validateRequire : []">
                    <el-input v-model="model.advisecomment" type="textarea" maxlength="1000" show-word-limit :autosize="{ minRows: 1, maxRows: 3 }"></el-input>
                  </el-form-item>
                </el-col>
              </el-row>
            </el-form>
            <el-form class="tl-form" ref="form" :model="model" :rules="rules" label-width="150px" :disabled="disableEditSubmit || disableSave">
              <div class="tl-form-line-title">
                反馈详情
              </div>
              <a id="feedback" class="step-position">&nbsp;</a>
              <el-row>
                <el-col :span="12">
                  <el-form-item label="项目名称" prop="projectname">
                    <el-input v-model="model.projectname"></el-input>
                  </el-form-item>
                </el-col>
                <el-col :span="12">
                  <el-form-item label="项目编号" prop="projectcode">
                    <el-input v-model="model.projectcode"></el-input>
                  </el-form-item>
                </el-col>
              </el-row>
              <el-row>
                <el-col :span="12">
                  <el-form-item label="拜访时间" prop="visitdate">
                    <el-date-picker v-model="model.visitdate" type="date" placeholder="拜访时间" value-format="yyyy-MM-dd"></el-date-picker>
                  </el-form-item>
                </el-col>
                <el-col :span="12">
                  <el-form-item label="专家岗位" prop="expertpost">
                    <el-input v-model="model.expertpost"></el-input>
                  </el-form-item>
                </el-col>
                <el-col :span="12">
                  <el-form-item label="我司拜访人员" prop="visitname">
                    <el-input v-model="model.visitname"></el-input>
                  </el-form-item>
                </el-col>
              </el-row>
              <el-row>
                <el-col :span="24">
                  <el-form-item label="专家意见" prop="expertadvise">
                    <el-input v-model="model.expertadvise" type="textarea" maxlength="1000" show-word-limit :autosize="{ minRows: 1, maxRows: 3 }"></el-input>
                  </el-form-item>
                </el-col>
              </el-row>
            </el-form>
            <div slot="footer" class="dialog-footer" style="float: right; margin-top: 10px">
              <el-button type="primary" @click="doSubmitEdit" :loading="saveLoading" v-if="!disableEditSubmit"><svg-icon icon-class="save" />保存</el-button>
              <el-button type="primary" @click="doSubmitApproval" :loading="submitLoading" v-if="!disableEditSubmit" :disabled="disableApproval"><svg-icon icon-class="send" />提交审批</el-button>
              <tl-attachment sourceType="MpmProjectVisitExpert" :sourceId="model.id" :disabled="disableAttachment" :editable="stateEdit"></tl-attachment>
              <el-button type="primary" @click="$refs.history.visible = true" :disabled="disableApprovalHistory"><svg-icon icon-class="list" />审批历史</el-button>
            </div>
          </div>
        </el-scrollbar>
      </div>
    </div>
    <tl-approval-history ref="history" sourceType="MpmProjectVisitExpert" :sourceId="model.id" />
  </tl-sub-page>
</template>

<script>
/** mixins */
import { mixin_approval_edit } from '@tapui/mixins/mixin_approval_edit'
/** 子组件 */
/** 工具类 */
import { putAction, getAction, postAction, deleteAction } from '@tapui/utils/http'
import { formatDate } from '@tapui/utils/date'
/** VUE配置 */
export default {
  mixins: [mixin_approval_edit],
  components: {},
  data() {
    return {
      moduleTitle: '评分细则专家拜访',
      urlPrefix: '/mpm/projectVisitExpert/',
      url: {
        // 额外的url
        queryProject: '/mpm/project/list'
      },
      mainHeight: 700,
      mainScrollElement: null,
      navList: [{ name: '填写内容', on: true, code: 'basic' }, { name: '反馈详情', on: false, code: 'feedback' }]
    }
  },
  props: {
    // subpage 布局属性 默认为dialog
    render: {
      default: 'el-drawer',
      type: String
    }
  },
  methods: {
    loaded() {
      // 页面加载完毕执行
      if (!this.isAdd && this.model.docno) {
        this.title = this.title + `（单据编号：${this.model.docno}）`
      }
      getAction(this.url.queryProject, { qry_projectcode: this.model.projectcode })
        .then((res) => {
          if (res.success) {
            this.$set(this.model, 'projectname', res.result.dataList[0].projectname)
          } else {
            this.$tap.alert(res.message, 'warning')
          }
        })
        .catch((err) => {
          this.$tap.alert(err.message, 'warning')
        })
    },
    /** 评标专家拜访反馈审批前调用 **/
    saveInfo(resolve, reject) {
      this.$refs.formable.validate((valid) => {
        if (valid) {
          putAction(this.url.edit, this.model)
            .then((res) => {
              if (res.success) {
                this.$emit('refresh')
                resolve()
              } else {
                this.$tap.alert(res.message, 'warning')
                reject()
              }
            })
            .catch((err) => {
              this.$tap.alert('填写内容失败，请联系系统管理员', 'warning')
              reject()
            })
        } else {
          reject()
        }
      })
    },
    /** 抽屉组件必有 **/
    resizeHandler() {
      this.mainHeight = document.body.getBoundingClientRect().height - 100 - (this.isApproval ? 110 : 0)
    },
    /** 抽屉组件必有 **/
    doCancelEdit() {
      this.visible = false
      this.$emit('close')
      this.$emit('refresh')
    },
    /** 抽屉组件必有 **/
    formatDate(date) {
      return formatDate(new Date(date ? date.toString().replace(/\-/g, '/') : date), 'yyyy-MM-dd')
    },
    /** 抽屉组件必重写，主要为了执行resizeHandler方法、给mainScrollElement赋值，并添加监听事件，不要直接复制该方法 **/
    open() {
      this.model = {}
      this.resizeHandler()
      this.$nextTick(() => {
        this.$refs.form.clearValidate()
        if (this.isAdd === true) {
          this.title = '新增' + this.moduleTitle
          // 需要添加重复判断
          if (this.$store.getters.param.isAvoidRepeat === true) {
            this.queryBillUid()
          }
          if (this.isQueryInitData === true) {
            // 后台进行数据初始化
            this.queryInitData().then((_) => {
              this.assignDefaultValue()
              this.loaded()
            })
          } else {
            // 前台进行数据初始化
            this.$set(this.model, 'enabled', 1)
            this.$set(this.model, 'sortcode', 0)
            this.assignDefaultValue()
            this.loaded()
          }
        } else if (this.disableEditSubmit === false) {
          this.title = '编辑' + this.moduleTitle
          this.queryData(this.id).then((_) => {
            this.$refs.form.clearValidate()
            this.loaded()
          })
        } else {
          this.title = '查看' + this.moduleTitle
          this.queryData(this.id).then((_) => {
            this.$refs.form.clearValidate()
            this.loaded()
          })
        }
        this.mainScrollElement = document.getElementById('mpmMainContainer').getElementsByClassName('el-scrollbar__wrap')[0]
        this.onMainScroll()
      })
    },
    /** 点击左侧导航，抽屉组件必有 **/
    clickNav(code) {
      this.navList.forEach((m) => {
        if (m.code === code) {
          m.on = true
        } else {
          m.on = false
        }
      })
      let element = document.getElementById(code)
      if (element === null) return
      let goY = element.getBoundingClientRect().y - 75
      this.mainScrollElement.scrollTop += goY
    },
    /** 页面滚动时，动态改变左侧导航选中的元素，抽屉组件必有 **/
    onMainScroll() {
      this.mainScrollElement.addEventListener('scroll', () => {
        let currentTop = this.mainScrollElement.scrollTop
        let code = ''
        for (let element of document.getElementsByClassName('step-position')) {
          if (element.getBoundingClientRect().y >= 0) {
            code = element.id
            // 滚动内容 距离底部大于10px
            if (this.mainScrollElement.scrollHeight - this.mainScrollElement.scrollTop - document.getElementById('mpmMainContainer').scrollHeight > 10) {
              break
            }
          }
        }
        this.navList.forEach((m) => {
          if (m.code === code) {
            m.on = true
          } else {
            m.on = false
          }
        })
      })
    },
    doSubmitEdit() {
      this.$tap.message('保存成功', 'success')
    }
  }
}
</script>
